package com.miyake.client.community;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.miyake.client.DoctorsUserService;
import com.miyake.client.DoctorsUserServiceAsync;
import com.miyake.client.community.PaperList.PaperListHandler;
import com.miyake.client.tools.ClickableLabel;
import com.miyake.client.tools.ImageListElement;
import com.miyake.client.tools.OkCancelDialog;
import com.miyake.client.tools.ShowDialog;
import com.miyake.client.tools.SmallButton;

public class CommunityDetail extends Composite {
	public interface CommunityDetailHandler {
		void onBack();
	}
	private List<CommunityDetailHandler> handlers = new ArrayList<CommunityDetailHandler>();
	public void addCommunityDetailHandler(CommunityDetailHandler handler) {
		handlers.add(handler);
	}
	protected void fireBackEvent() {
		for (CommunityDetailHandler handler : handlers) {
			handler.onBack();
		}
	}
	protected DoctorsUserServiceAsync service = DoctorsUserService.Util.getInstance();
	private PaperList papers;
	
	public CommunityDetail(final ImageListElement prop) {
		
		VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setWidth("100%");
		verticalPanel.add(new HTML("<h1>" + prop.getName()+ "</h1>"));
		
		VerticalPanel toolBar = new VerticalPanel();
		verticalPanel.add(toolBar);
		
		
		final VerticalPanel content = new VerticalPanel();
		verticalPanel.add(content);
		
		Label back = new ClickableLabel("Back to community list");
		back.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				fireBackEvent();
			}
			
		});
		toolBar.add(back);
		
		Button delete = new SmallButton("Delete this community");
		delete.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				new ShowDialog() {
					@Override
					protected void onOK(String text) {
						deleteCommunity(prop, content);
					}

					@Override
					protected DialogBox getDialogBox() {
						return new OkCancelDialog();
					}
				};
			}
		});
		toolBar.add(delete);
		
		update(content);
	}
	private void update(VerticalPanel verticalPanel) {
		verticalPanel.clear();
		papers = new PaperList();
		verticalPanel.add(papers);
		verticalPanel.add(new MemberList() {
			@Override
			protected boolean canCreate() {
				return true;
			}

			@Override
			protected boolean canFind() {
				return true;
			}
			
		});
	}
	
	public void addPaperListHandler(PaperListHandler handler) {
		papers.addPalerListHandler(handler);
	}
	
	private void deleteCommunity(ImageListElement prop, final VerticalPanel content) {
		service.deleteCommunity(prop.getKey(), new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(Void result) {
				fireBackEvent();
			}
		});
	}
}
